package com.credit.util;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

import com.credit.entity.CreditLoanOrder;

public class ReflectionUtil {
	
	
	private ReflectionUtil(){
		throw new AssertionError();
	}

	public static Object getValue(Object target,String fieldName) throws Exception{
		
		Class classz = target.getClass();
		String[] fields = fieldName.split("\\.");
		
		try{
			for(int i=0;i<fields.length;i++){
				Field field = classz.getDeclaredField(fields[i]);
				field.setAccessible(true);
				target = field.get(target);
				classz = target.getClass();
			}
			
			Field f = classz.getDeclaredField(fields[fieldName.length()-1]);
			f.setAccessible(true);
			return f.get(target);
		}catch(Exception e){
			throw new RuntimeException(e);
		}
		
	}
	
	
	public static void setValue(Object target,String fieldName,Object value){
		
		Class classz = target.getClass();
		String[] fields = fieldName.split("\\.");
		
		try{
			
			for(int i=0;i<fields.length;i++){
				Field field = classz.getDeclaredField(fields[i]);
				field.setAccessible(true);
				Object val = field.get(target);
				if(val == null){
					Constructor c = field.getType().getDeclaredConstructor();
					c.setAccessible(true);
					val = c.newInstance();
					field.set(target, val);
				}
				target = val;
				classz = target.getClass();
			}
			
			Field f = classz.getDeclaredField(fields[fieldName.length()-1]);
			f.setAccessible(true);
			f.set(target, value);
			
		}catch(Exception e){
			throw new RuntimeException(e);
		}
		
	}
	
	
	public static void main(String[] args) throws Exception {
		/*String str = "hello";
		Method method = str.getClass().getMethod("toUpperCase");
		System.out.println(method.invoke(str));*/
		
		CreditLoanOrder loanOrder = new CreditLoanOrder();
		//loanOrder.setCreditType(4);
		System.out.println(loanOrder.getCreditType());
		setValue2(loanOrder,"creditType","23");
		System.out.println(loanOrder.getCreditType());
		
	}

	public static <T> void setProperties(HttpServletRequest request, T t) {
		
		Enumeration<String> enumeration = request.getParameterNames();
		while(enumeration.hasMoreElements()){
			String key = enumeration.nextElement();
			String value = request.getParameter(key);
			if(!StringUtils.isEmpty(value)){
				setValue2(t,key,value);
			}
		}
		
	}
	
	 /** 
     * 取Bean的属性和值对应关系的MAP 
     *  
     * @param bean 
     * @return Map 
     */  
    public static Map<String, String> getFieldValueMap(Object bean) {  
        Class<?> cls = bean.getClass();  
        Map<String, String> valueMap = new HashMap<String, String>();  
        Method[] methods = cls.getDeclaredMethods();  
        Field[] fields = cls.getDeclaredFields();  
        for (Field field : fields) {  
            try {  
                String fieldType = field.getType().getSimpleName();  
                String fieldGetName = parGetName(field.getName());  
                if (!checkGetMet(methods, fieldGetName)) {  
                    continue;  
                }  
                Method fieldGetMet = cls.getMethod(fieldGetName, new Class[] {});  
                Object fieldVal = fieldGetMet.invoke(bean, new Object[] {});  
                String result = null;  
                if ("Date".equals(fieldType)) {  
                    result = fmtDate((Date) fieldVal);  
                } else {  
                    if (null != fieldVal) {  
                        result = String.valueOf(fieldVal);  
                    }  
                }  
//              String fieldKeyName = parKeyName(field.getName());  
                valueMap.put(field.getName(), result);  
            } catch (Exception e) {  
                continue;  
            }  
        }  
        return valueMap;  
    }  
	
	/** 
     * set属性的值到Bean 
     *  
     * @param bean 
     * @param valMap 
     */  
    public static void setFieldValue(Object bean, Map<String, String> valMap) {  
        Class<?> cls = bean.getClass();  
        // 取出bean里的所有方法  
        Method[] methods = cls.getDeclaredMethods();  
        Field[] fields = cls.getDeclaredFields();  
  
        for (Field field : fields) {  
            try {  
                String fieldSetName = parSetName(field.getName());  
                if (!checkSetMet(methods, fieldSetName)) {  
                    continue;  
                }  
                Method fieldSetMet = cls.getMethod(fieldSetName,  
                        field.getType());  
//              String fieldKeyName = parKeyName(field.getName());  
                String  fieldKeyName = field.getName();  
                String value = valMap.get(fieldKeyName);  
                if (null != value && !"".equals(value)) {  
                    String fieldType = field.getType().getSimpleName();  
                    if ("String".equals(fieldType)) {  
                        fieldSetMet.invoke(bean, value);  
                    } else if ("Date".equals(fieldType)) {  
                        Date temp = parseDate(value);  
                        fieldSetMet.invoke(bean, temp);  
                    } else if ("Integer".equals(fieldType)  
                            || "int".equals(fieldType)) {  
                        Integer intval = Integer.parseInt(value);  
                        fieldSetMet.invoke(bean, intval);  
                    } else if ("Long".equalsIgnoreCase(fieldType)) {  
                        Long temp = Long.parseLong(value);  
                        fieldSetMet.invoke(bean, temp);  
                    } else if ("Double".equalsIgnoreCase(fieldType)) {  
                        Double temp = Double.parseDouble(value);  
                        fieldSetMet.invoke(bean, temp);  
                    } else if ("Boolean".equalsIgnoreCase(fieldType)) {  
                        Boolean temp = Boolean.parseBoolean(value);  
                        fieldSetMet.invoke(bean, temp);  
                    }else if ("BigDecimal".equalsIgnoreCase(fieldType)) {  
                    	BigDecimal temp = new BigDecimal(value);
                        fieldSetMet.invoke(bean, temp);  
                    } else {  
                        System.out.println("not supper type" + fieldType);  
                    }  
                }  
            } catch (Exception e) {  
                continue;  
            }  
        }  
    }  
    
    /** 
     * 格式化string为Date 
     *  
     * @param datestr 
     * @return date 
     */  
    public static Date parseDate(String datestr) {  
        if (null == datestr || "".equals(datestr)) {  
            return null;  
        }  
        try {  
            String fmtstr = null;  
            if (datestr.indexOf(':') > 0) {  
                fmtstr = "yyyy-MM-dd HH:mm:ss";  
            } else {  
                fmtstr = "yyyy-MM-dd";  
            }  
            SimpleDateFormat sdf = new SimpleDateFormat(fmtstr, Locale.UK);  
            return sdf.parse(datestr);  
        } catch (Exception e) {  
            return null;  
        }  
    }  
  
    /** 
     * 日期转化为String 
     *  
     * @param date 
     * @return date string 
     */  
    public static String fmtDate(Date date) {  
        if (null == date) {  
            return null;  
        }  
        try {  
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",  
                    Locale.US);  
            return sdf.format(date);  
        } catch (Exception e) {  
            return null;  
        }  
    }  
  
    /** 
     * 判断是否存在某属性的 set方法 
     *  
     * @param methods 
     * @param fieldSetMet 
     * @return boolean 
     */  
    public static boolean checkSetMet(Method[] methods, String fieldSetMet) {  
        for (Method met : methods) {  
            if (fieldSetMet.equals(met.getName())) {  
                return true;  
            }  
        }  
        return false;  
    }  
  
    /** 
     * 判断是否存在某属性的 get方法 
     *  
     * @param methods 
     * @param fieldGetMet 
     * @return boolean 
     */  
    public static boolean checkGetMet(Method[] methods, String fieldGetMet) {  
        for (Method met : methods) {  
            if (fieldGetMet.equals(met.getName())) {  
                return true;  
            }  
        }  
        return false;  
    }  
  
    /** 
     * 拼接某属性的 get方法 
     *  
     * @param fieldName 
     * @return String 
     */  
    public static String parGetName(String fieldName) {  
        if (null == fieldName || "".equals(fieldName)) {  
            return null;  
        }  
        int startIndex = 0;  
        if (fieldName.charAt(0) == '_')  
            startIndex = 1;  
        return "get"  
                + fieldName.substring(startIndex, startIndex + 1).toUpperCase()  
                + fieldName.substring(startIndex + 1);  
    }  
  
    /** 
     * 拼接在某属性的 set方法 
     *  
     * @param fieldName 
     * @return String 
     */  
    public static String parSetName(String fieldName) {  
        if (null == fieldName || "".equals(fieldName)) {  
            return null;  
        }  
        int startIndex = 0;  
        if (fieldName.charAt(0) == '_')  
            startIndex = 1;  
        return "set"  
                + fieldName.substring(startIndex, startIndex + 1).toUpperCase()  
                + fieldName.substring(startIndex + 1);  
    }  
  
    /** 
     * 获取存储的键名称（调用parGetName） 
     *  
     * @param fieldName 
     * @return 去掉开头的get 
     */  
    public static String parKeyName(String fieldName) {  
        String fieldGetName = parGetName(fieldName);  
        if (fieldGetName != null && fieldGetName.trim() != ""  
                && fieldGetName.length() > 3) {  
            return fieldGetName.substring(3);  
        }  
        return fieldGetName;  
    }  
	
	public static void setValue2(Object target,String fieldName,Object value){
		
		
		
		try{
			    Class classz = target.getClass();
				Field field = classz.getDeclaredField(fieldName);
				field.setAccessible(true);
				
				System.out.println();
				
				Object object = field.getType().getDeclaredConstructor().newInstance(value);
		
				
				field.set(target, object);
			
		}catch(Exception e){
			throw new RuntimeException(e);
		}
		
	}
	
}
